﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DotSpatial.Topology;
using DotSpatial.Data;


namespace FXnaGis
{

    [Serializable]
    /// <summary>
    /// 为分组提供类支持
    /// </summary>
    public class FGroupLayer:FLayer
    {

        public FGroupLayer(string id)
            : base(id)
        {
 
        }



        #region 事件

        [field:NonSerialized]
        /// <summary>
        /// Occurs when a layer is added to this item.
        /// </summary>
        public event EventHandler<LayerEventArgs> LayerAdded;


         [field: NonSerialized]
        /// <summary>
        /// Occurs when a layer is removed from this item.
        /// </summary>
        public event EventHandler<LayerEventArgs> LayerRemoved;



        #endregion


         #region 属性字段

         public override FMapFrame MapFrame
         {
             get
             {
                 return base.MapFrame;
             }
             set
             {

                 base.MapFrame = value;

                 //需要更新自己的孩子

                 foreach (FLayer layer in this._children)
                 {
                     layer.MapFrame = value;
                     
                 }


             }
         }

         public override DotSpatial.Data.Extent Extent
         {
             get
             {

                 Extent ext = null;

                 foreach (FLayer layer in this._children)
                 {
                     if (layer.Extent != null)
                     {
                         if (ext == null)
                         {
                             ext = (Extent)layer.Extent.Clone();
                         }
                         else
                         {
                             ext.ExpandToInclude(layer.Extent);
                         }
                     }
                 }

                 return ext;

             }
         }


         public override Extent SelectedExtent
         {
             get
             {
                 Extent ext = null;

                 foreach (FLayer layer in this._children)
                 {
                     if (layer.SelectedExtent != null)
                     {
                         if (ext == null)
                         {
                             ext = (Extent)layer.SelectedExtent.Clone();
                         }
                         else
                         {
                             ext.ExpandToInclude(layer.SelectedExtent);
                         }
                     }
                 }

                 return ext;


             }
         }



         List<FLayer> _children = new List<FLayer>();

         public List<FLayer> Children
         {
             get { return _children; }
            
         }


         #endregion




        public override void DrawRegions(FMapArgs args, List<DotSpatial.Data.Extent> regions)
        {
            foreach (FLayer layer in this._children)
            {
                layer.DrawRegions(args,regions);
                
            }


        }




        protected override void OnSetVisible()
        {
            base.OnSetVisible();
        }


        #region 对外功能 函数

        public void Add(FLayer layer)
        {

           

            this._children.Add(layer);

            this.OnAddLayer(layer);


        }





        /// <summary>
        /// 移除一个图层
        /// </summary>
        /// <param name="layer"></param>
        public void Remove(FLayer layer)
        {

            

            this._children.Remove(layer);

            this.OnRemoveLayer(layer);



        }


        protected void OnAddLayer(FLayer layer)
        {
            layer.Parent = this;

            if (this.LayerAdded!=null)
            {
                this.LayerAdded(this,new LayerEventArgs (layer));
                
            }

            if (this.MapFrame!=null)
            {

                this.MapFrame.RedrawBuffer();
                
            }
 


        }


        protected void OnRemoveLayer(FLayer layer)
        {

            layer.Parent = null;

            if (this.LayerRemoved!=null)
            {
                this.LayerRemoved(this,new LayerEventArgs(layer));
                
            }

            if (this.MapFrame!=null)
            {
                this.MapFrame.RedrawBuffer();
                
            }
 

        }
  







        public void Remove(string id)
        {
            FLayer layer = this.GetLayer(id);

            if (layer!=null)
            {


                this.Remove(layer);
               

                

                
            }

 
        }


       



        public FLayer GetLayer(string id)
        {

            foreach (FLayer layer in this._children)
            {
                if (layer.Id==id)
                {
                    return layer;
                    
                }


            

                if (layer is FGroupLayer)
                {
                    if ((layer as FGroupLayer).GetLayer(id) != null)
                    {
                        return (layer as FGroupLayer).GetLayer(id);

                    }


                }

                 
                
            }

            return null;

        }


        public List<FVectorLayer> GetVectorLayers()
        {
            List<FVectorLayer> layers = new List<FVectorLayer>();

            foreach (FLayer layer in this._children)
            {
                if (layer is FVectorLayer)
                {
                    layers.Add((FVectorLayer)layer);

                }
                else
                {
                    if (layer is FVectorLayer)
                    {

                        FGroupLayer glayer = layer as FGroupLayer;

                        layers.AddRange(glayer.GetVectorLayers());
                        
                    }
                }
                
            }

            return layers;
        }


        #endregion


        #region 选择函数

        public override void Select(Extent selectedArea, FSelectionMode smode, FSelectionOperationMode omode)
        {
            base.Select(selectedArea, smode, omode);

            foreach (FLayer item in this._children)
            {
                item.Select(selectedArea,smode,omode);
                
            }



        }

        #endregion


    }
}
